package com.yami.shop.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.dto.UserCollectionShopDto;
import com.yami.shop.bean.model.UserCollectionShop;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.UserCollectionShopService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author LGH
 */
@RestController
@RequestMapping("/p/shop/collection")
@Tag(name = "店铺收藏接口")
@AllArgsConstructor
public class UserCollectionShopController {

    private final UserCollectionShopService userCollectionShopService;

    private final ShopDetailService shopDetailService;

    @GetMapping("/page")
    @Operation(summary =  "分页返回收藏数据", description = "根据用户id获取")
    @Parameter(name = "shopName", description = "店铺名称")
    public ServerResponseEntity<IPage<UserCollectionShopDto>> getUserCollectionDtoPageByUserId(PageParam page, String shopName) {
        return ServerResponseEntity.success(userCollectionShopService.getUserCollectionShopPageByUserId(page, SecurityUtils.getUser().getUserId(), shopName));
    }

    @GetMapping("isCollection")
    @Operation(summary =  "根据店铺id获取该店铺是否在收藏夹中", description = "传入收藏店铺id")
    @Parameter(name = "shopId", description = "店铺id")
    public ServerResponseEntity<Boolean> isCollection(Long shopId) {
        return ServerResponseEntity.success(userCollectionShopService.count(new LambdaQueryWrapper<UserCollectionShop>()
                .eq(UserCollectionShop::getShopId, shopId)
                .eq(UserCollectionShop::getUserId, SecurityUtils.getUser().getUserId())) > 0);
    }

    @PostMapping("/addOrCancel")
    @Operation(summary =  "添加/取消收藏", description = "传入收藏店铺id,如果店铺未收藏则收藏店铺，已收藏则取消收藏")
    @Parameter(name = "shopId", description = "店铺id", required = true)
    public ServerResponseEntity<Boolean> addOrCancel(@RequestBody Long shopId) {
        if (Objects.isNull(shopDetailService.getById(shopId))) {
            // 该店铺不存在
            throw new YamiShopBindException("yami.store.not.exist");
        }
        boolean isAdd = false;
        String userId = SecurityUtils.getUser().getUserId();
        if (userCollectionShopService.count(new LambdaQueryWrapper<UserCollectionShop>()
                .eq(UserCollectionShop::getShopId, shopId)
                .eq(UserCollectionShop::getUserId, userId)) > 0) {
            userCollectionShopService.remove(new LambdaQueryWrapper<UserCollectionShop>()
                    .eq(UserCollectionShop::getShopId, shopId)
                    .eq(UserCollectionShop::getUserId, userId));
        } else {
            UserCollectionShop userCollectionShop = new UserCollectionShop();
            userCollectionShop.setCreateTime(new Date());
            userCollectionShop.setUserId(userId);
            userCollectionShop.setShopId(shopId);
            userCollectionShopService.save(userCollectionShop);
            isAdd = true;
        }
        return ServerResponseEntity.success(isAdd);
    }

    @PostMapping("/batachCancel")
    @Operation(summary =  "批量取消收藏", description = "传入收藏店铺id")
    @Parameter(name = "shopIds", description = "店铺Id", required = true)
    public ServerResponseEntity<Boolean> batchCancel(@RequestBody List<Long> shopIds) {
        String userId = SecurityUtils.getUser().getUserId();
        if (CollectionUtils.isEmpty(shopIds)) {
            return ServerResponseEntity.success(false);
        }
        boolean remove = false;
        for (Long shopId : shopIds) {
            if (Objects.isNull(shopDetailService.getById(shopId))) {
                continue;
            }
            long count = userCollectionShopService.count(new LambdaQueryWrapper<UserCollectionShop>()
                    .eq(UserCollectionShop::getShopId, shopId)
                    .eq(UserCollectionShop::getUserId, userId));
            if (count <= 0) {
                continue;
            }
            remove = userCollectionShopService.remove(new LambdaQueryWrapper<UserCollectionShop>()
                    .eq(UserCollectionShop::getShopId, shopId)
                    .eq(UserCollectionShop::getUserId, userId));
        }
        return ServerResponseEntity.success(remove);
    }

    @GetMapping("count")
    @Operation(summary =  "查询用户收藏店铺数量", description = "查询用户收藏店铺数量")
    public Long findUserCollectionCount() {
        String userId = SecurityUtils.getUser().getUserId();
        return userCollectionShopService.count(new LambdaQueryWrapper<UserCollectionShop>().eq(UserCollectionShop::getUserId, userId));
    }
}
